<?php
class BaseAction extends Action {
	
	function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}

	function _initialize(){
		$this->initAction();
        $this->InitAuthority();
		$this->initMenu();
		$this->initModule();
	}
	
	protected function initAction(){
       
		if (session('userid') == null
			|| session('username') == null){

			if (cookie('keepLoged') == true 
				&& cookie('userid') != null
				&& cookie('username')!= null){
				session('userid',cookie('userid'));
				session('username',cookie('username'));
				session('departid',cookie('departid'));
			}
			else{
				$this->redirect('/User/login', null,0,'您还没有登录！');
			}
		}
	}
        
        protected function InitAuthority(){

            $this->get_authority(MODULE_NAME,ACTION_NAME, 'View');
        }
        
        protected function get_authority($module,$action,$name){
            
            $condition['Module'] = $module;
            $condition['Action'] = $action;

            $menuid = M('menu')->where($condition)->getField('Id');

            if(!isset($menuid)){
                return;
            }
            
            $d = M('user')->field('UserLevel')->find((int)session('userid'));
        	$userlevel = $d['UserLevel'];

            $condition['Type'] = 1;
            $condition['LevelId'] = $userlevel;
            $condition['MenuId'] = $menuid;

            $result = M('userauthority')->where($condition)->getField($name);

            if (!$result){
                $this->error('您没有相应权限!');
            }
        }
	
	protected function get_menu(){
		$parent = M('Menu')->where('Pid = ""')->select();
		foreach ($parent as $n => $val){
			$map['Pid'] = $parent[$n]['Id'];
			$parent[$n]['voo'] = M('Menu')->where($map)->select();
		}
		return $parent;
	}
	
	protected  function initMenu(){
		//$menu = $this->get_menu();
		$menu = $this->get_modules_by_userid((int)session('userid'));
		$this->assign('menu',$menu);
		
	}
	
	public function get_modules_by_userid($userid){
        
        if(!isset($userid)){
                 return NULL;
         }
        $d = M('user')->field('UserLevel')->find($userid);
        $userlevel = $d['UserLevel'];
        $parent = M('userauthority')->join('menu on Id=MenuId')
                ->where("Type=1 and ifnull(Pid,0) = 0 and LevelId=$userlevel and userauthority.View = 1 and menu.View=1")
                ->order('Id')->select();
        
        foreach ($parent as $n => $val){
                
                $map['Type'] = 1;
                $map['LevelId'] = $userlevel;
                $map['menu.Pid'] = $val['Id'];
                $map['menu.View'] = 1;
                $map['userauthority.View'] = 1;
                
                $parent[$n]['voo'] = M('userauthority')->join('menu on Id=MenuId')->where($map)->order('Id')->select();
                //print M('userauthority')->getLastSql();exit;
                //print_r($parent[$n]['voo']);exit;
        }
        return $parent;
    }
	
	final  protected  function  initModule(){
		$map['Module'] = $this->_param(0);
		$map['Action'] = $this->_param(1);
		
		$title = M('Menu')->where($map)->select();
		
		if ($title == ""){
			$title = M('Menu')->where("Module='".$this->_param(0)."'")->select();
		}
		
		if ($title == ""){
			$this->assign('title', "控制台");
		}
		else{
			$this->assign('title',$title[0]['MenuName']);
			
		}
		
	}
	
	
}